package com.conop.gradient;

public class BallSet implements ConvexSet 
{
	private double _radius;
	private NDPoint _center;
	
	public BallSet(int dim)
	{
		_radius = 1;
		_center = new NDPoint(0, dim);
	}
	
	public BallSet()
	{
		this(2);
	}
	
	public BallSet(NDPoint center, double radius)
	{
		_center = new NDPoint(center);
		_radius = radius;
	}
	
	public BallSet(double[] arr, double radius)
	{
		_center = new NDPoint(arr);
		_radius = radius;
	}
	
	@Override
	public boolean isFeasible(NDPoint pnt) throws IllegalArgumentException 
	{
		return (pnt.Substraction(_center).Norm() <= _radius);
	}

	@Override
	public NDPoint Projection(NDPoint pnt) throws IllegalArgumentException
	{
		return this.isFeasible(pnt) ?
					pnt :
					pnt.Substraction(_center).Multiply(_radius/pnt.Substraction(_center).Norm()).Addition(_center);
	}
	
	@Override
	public String toString()
	{
		String str = this._center.toString().concat("D Ball: \n");
		
		str = str.concat("\tCenter: ").concat(this._center.toString()).concat("\n")
				 .concat("\tRadius: ").concat(Double.toString(this._radius));
		
		return str;
	}
}
